//
//  JDTabBarController.swift
//  DragonWallet
//
//  Created by volantPig on 2023/12/26.
//

import UIKit

class JDTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        self.delegate = self
        self.setupTabBar()
        // Do any additional setup after loading the view.
    }
    
    func setupTabBar() {
        
        self.setupTabbarDetail()
//        self.addNotification()
        self.setupTabbarConfig()
    }
    
    func setupTabbarDetail() {
//        let outlandSalesVC = JDOutlandSalesViewController()
//        let outlandSales = self.tabBarAddChildVC(childVC: outlandSalesVC, childTitle: "境外直销", imageName: "tabbar_outland_unselected", selectedImageName: "tabbar_outland_selected", index: 0)
        
        /// 新版设计
        let outlandSalesVC = JDNewSaleListViewController()
        let outlandSales = self.tabBarAddChildVC(childVC: outlandSalesVC, childTitle: "境外直销", imageName: "tabbar_outland_unselected", selectedImageName: "tabbar_outland_selected", index: 0)
        
        
        
        let mainScreenVC = JDMainScreenViewController()
        let mainScreen = self.tabBarAddChildVC(childVC: mainScreenVC, childTitle: "尨看板", imageName: "tabbar_mainScreen_unselected", selectedImageName: "tabbar_mainScreen_selected", index: 1)
        
        let cityUnionVC = JDCityUnionViewController()
        let cityUnion = self.tabBarAddChildVC(childVC: cityUnionVC, childTitle: "尨商圈", imageName: "tabbar_cityUnion_unselected", selectedImageName: "tabbar_cityUnion_selected", index: 2)
        
        let mineVC = JDMineViewController()
        let mine = self.tabBarAddChildVC(childVC: mineVC, childTitle: "我的", imageName: "tabbar_mine_unselected", selectedImageName: "tabbar_mine_selected", index: 3)
        
        self.viewControllers = [outlandSales, mainScreen, cityUnion, mine]
        
    }
    
    func setupOldTabbarDetail() {
        let outlandSalesVC = JDOutlandSalesViewController()
        let outlandSales = self.tabBarAddChildVC(childVC: outlandSalesVC, childTitle: "境外直销", imageName: "tabbar_outland_unselected", selectedImageName: "tabbar_outland_selected", index: 0)
        
        let mainScreenVC = JDMainScreenViewController()
        let mainScreen = self.tabBarAddChildVC(childVC: mainScreenVC, childTitle: "尨看板", imageName: "tabbar_mainScreen_unselected", selectedImageName: "tabbar_mainScreen_selected", index: 1)
        
        let shopcartVC = JDShopcartViewController()
        let shopcart = self.tabBarAddChildVC(childVC: shopcartVC, childTitle: "购物车", imageName: "tabbar_shopcart_unselected", selectedImageName: "tabbar_shopcart_selected", index: 2)
        
        let messageVC = JDMessageViewController()
        let message = self.tabBarAddChildVC(childVC: messageVC, childTitle: "商务快讯", imageName: "tabbar_message_unselected", selectedImageName: "tabbar_message_selected", index: 3)
        
        let cityUnionVC = JDCityUnionViewController()
        let cityUnion = self.tabBarAddChildVC(childVC: cityUnionVC, childTitle: "尨商圈", imageName: "tabbar_cityUnion_unselected", selectedImageName: "tabbar_cityUnion_selected", index: 4)
        
        self.viewControllers = [outlandSales, mainScreen, shopcart, message, cityUnion]
    }
    
    func setupTabbarConfig() {
        self.tabBar.unselectedItemTintColor = .black
        
        self.tabBar.tintColor = color("#FA6161")

        let bgImgV = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: screenWidth, height: UIDevice().safeAreaInset.bottom))
        bgImgV.image = JDTools.customImageWithColor(color: .white, alpha: 1)
        let effect = UIBlurEffect.init(style: .light)
        let effectView = UIVisualEffectView.init(effect: effect)
        effectView.frame = CGRect.init(x: 0, y: 0, width: screenWidth, height: UIDevice().safeAreaInset.bottom)
        bgImgV.addSubview(effectView)
        self.tabBar.insertSubview(bgImgV, at: 0)
        bgImgV.alpha = 1
        self.tabBar.backgroundImage = bgImgV.image
    }
    
    func tabBarAddChildVC(childVC: UIViewController, childTitle: String, imageName: String, selectedImageName: String, index: Int) -> UIViewController {
        
        childVC.tabBarItem.title = childTitle
        childVC.tabBarItem.tag = index
        childVC.tabBarItem.image = UIImage.init(named: imageName)?.withRenderingMode(.alwaysOriginal)
        childVC.tabBarItem.selectedImage = UIImage.init(named: selectedImageName)?.withRenderingMode(.alwaysOriginal)
        
        let nvc = JDNavigationController.init(rootViewController: childVC)
        
        return nvc
    }
    
    
    
//    func setupTabBar() {
//
//        let mainScreenVC = JDMainScreenViewController()
//        let mainScreen = self.tabBarAddChildVC(childVC: mainScreenVC, childTitle: "龙看板", imageName: "", selectedImageName: "tabbar_mainScreen_seelected", index: 0)
//
//        let cityShareVC = JDCityShareViewController()
//        let cityShare = self.tabBarAddChildVC(childVC: cityShareVC, childTitle: "同城分享", imageName: "tabbar_cityShare_unselected", selectedImageName: "", index: 1)
//
//        let messageVC = JDMessageViewController()
//        let message = self.tabBarAddChildVC(childVC: messageVC, childTitle: "商务快讯", imageName: "tabbar_message_unselected", selectedImageName: "", index: 2)
//
//        let cityUnionVC = JDCityUnionViewController()
//        let cityUnion = self.tabBarAddChildVC(childVC: cityUnionVC, childTitle: "龙商圈", imageName: "tabbar_cityUnion_unselected", selectedImageName: "", index: 3)
//
//        self.viewControllers = [mainScreen, cityShare, message, cityUnion]
//
//        self.tabBar.unselectedItemTintColor = .black
//
////        self.tabBar.tintColor = color("#00a0e9")
////
////        let bgImgV = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: screenWidth, height: UIDevice().safeAreaInset.bottom))
////        bgImgV.image = WJBTools.customImageWithColor(color: .white, alpha: 1)
////        let effect = UIBlurEffect.init(style: .light)
////        let effectView = UIVisualEffectView.init(effect: effect)
////        effectView.frame = CGRect.init(x: 0, y: 0, width: screenWidth, height: UIDevice().safeAreaInset.bottom)
////        bgImgV.addSubview(effectView)
////        self.tabBar.insertSubview(bgImgV, at: 0)
////        bgImgV.alpha = 1
////        self.tabBar.backgroundImage = bgImgV.image
////
////        self.addNotification()
//    }
//
//    func tabBarAddChildVC(childVC: UIViewController, childTitle: String, imageName: String, selectedImageName: String, index: Int) -> UIViewController {
//
//        childVC.tabBarItem.title = childTitle
//        childVC.tabBarItem.tag = index
//        childVC.tabBarItem.image = UIImage.init(named: imageName)?.withRenderingMode(.alwaysOriginal)
//        childVC.tabBarItem.selectedImage = UIImage.init(named: selectedImageName)?.withRenderingMode(.alwaysOriginal)
//        let nvc = JDNavigationController.init(rootViewController: childVC)
//
//        return nvc
//    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension JDTabBarController: UITabBarControllerDelegate {
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        
//        JDLoginViewController
        if (tabBarController.tabBar.selectedItem?.title == "尨商圈" || tabBarController.tabBar.selectedItem?.title == "龙看板" || tabBarController.tabBar.selectedItem?.title == "我的") && ((JDLoginModel.default.token ?? "").count == 0) {
            let loginVC = JDLoginViewController()
            let navigationLoginVC = UINavigationController(rootViewController: loginVC)
//            self.presentVC(navigationLoginVC)
            self.present(navigationLoginVC, animated: true, completion: nil)
            return false
        }
        return true
    }
    

}
